package com.hhs.storage.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.BaseUserService;
import com.hhs.storage.service.XmPptService;
import com.hhs.storage.service.XmSchoolService;
import com.hhs.storage.util.FileUpload;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chx
 * @since 2024-10-19
 */
@RestController
@RequestMapping("xmschool")
@Slf4j
public class XmSchoolController {
    @Resource
    private XmSchoolService xmSchoolService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private HttpServletResponse response;

    @Resource
    private BaseUserService baseUserService;

    @Value("${upload.folderName}")
    private String folderName;

    @RequestMapping("edit")
    public Map<String,Object> edit(XmSchool xmSchool){
        if(StringUtil.iszNull(xmSchool.getId())){
            xmSchool.setCreateTime(LocalDateTime.now());
        }
        xmSchoolService.saveOrUpdate(xmSchool);
        return ResponseInfo.success();
    }
    @RequestMapping("table")
    public Map<String,Object> table(InputParam inputParam){
        BaseUser byId = baseUserService.getById(SysUtil.getLoginUserId(request));
        Map<String,Object> map=new HashMap<>();
        if(!"2".equals(byId.getId())){
            XmSchool byIdSchool = xmSchoolService.getById(byId.getSchoolId());
            List r=new ArrayList();
            if(byIdSchool!=null){
                r.add(byIdSchool);
            }
            map.put("code",1);
            map.put("data",r);
            map.put("total",r.size());
            return map;
        }
        QueryWrapper qw=new QueryWrapper();
        qw.orderByDesc("create_time");
        inputParam.mapParams("name");
        if(!StringUtil.iszNull(inputParam.getName())){
            qw.like("name", inputParam.getName());
        }
        Page mp = xmSchoolService.page(new Page(inputParam.getPage(), inputParam.getSize()),qw);
        map.put("code",1);
        map.put("data",mp.getRecords());
        map.put("total",mp.getTotal());
        return map;
    }
    @RequestMapping("dele")
    public Map<String,Object> dele(XmSchool xmSchool){
        if(StringUtil.iszNull(xmSchool.getId())){
            return ResponseInfo.error("没有id,参数错误");
        }
        xmSchoolService.removeById(xmSchool.getId());
        return ResponseInfo.success();
    }

}

